//切换空基遥测相关的地图场景交互
export default class Kjyc {
    constructor(_viewer, points) {
        
        this.viewer = _viewer;
        this.cameraInfo = {
            lng: 116.77075872717633,
            lat: 39.795437056239649, alt:40
        };
        this.pathList = points;
        this.flyHeihgt = 40;
    }
    initFly() {
        // this.viewer.camera.flyTo(
        //     {
        //         // destination: window.Cesium.Cartesian3.fromDegrees(this.cameraInfo.lng, this.cameraInfo.lat, this.cameraInfo.alt),
        //         destination: window.Cesium.Cartesian3.fromDegrees(this.pathList[0].lon, this.pathList[0].lat, this.pathList[0].height || this.flyHeihgt),
        //         orientation: {
        //             heading: window.Cesium.Math.toRadians(42.74),
        //             pitch: window.Cesium.Math.toRadians(-56.95),
        //             roll: 0.0
        //         },
        //         complete: () => {
        //             this.doFlyByLine();
        //         }
        //     }
        // );
        this.doFlyByLine();
    }

    doFlyByLine() {
        let speed = 50;
        let flyHeihgt = 0;
        let pathInfo = [[
            116.77075872717633, 39.795437056239649,40
        ],
        [
            116.78067217169537,39.783721167262598, 40
        ],
        [
            116.78101549444928,39.77543850582461,40
        ],
        [
            116.77964220343365, 39.772219855006739, 40
        ],
        [
            116.7765522986485, 39.770589071925684, 40
        ],
        [
            116.77597294150128, 39.769258696254298, 40
        ],
        [
            116.78095112143292, 39.767048556026026, 40
        ],
        [
            116.78095112143292, 39.759538370784327, 40
        ],
        [
            116.78056488333478, 39.757907587703272, 40
        ],
        [
            116.78035030661358, 39.756148058589503, 40
        ],
        [
            116.78524265585675, 39.746921259578272, 40
        ],
        [
            116.78459892569317, 39.744775492366358, 40
        ],
        [
            116.78580055533185, 39.741299349483057, 40
        ],

        [
            116.78803215323224, 39.740183550532862, 40
        ],[
            116.79116497336163, 39.739453989680811, 40
        ],[
            116.7934824019505, 39.737651545222803, 40
        ],[
            116.79717312155499, 39.736063677485987, 40
        ],[
            116.80000553427472, 39.731900889094874, 40
        ],[
            116.80712948141827, 39.726708132442042, 40
        ],[
            116.80863151846661, 39.726922709163233, 40
        ],[
            116.8148971587254, 39.729304510768458, 40
        ],
        [
            116.81577692328229, 39.729626375850245, 40
        ],[
            116.81659231482281, 39.729948240932032, 40
        ],
        [
            116.77075872717633, 39.795437056239649, 40
        ]];
        pathInfo = [];
        this.pathList.forEach(item => {
            pathInfo.push([item.lon, item.lat, item.height || this.flyHeihgt])
        })

        this.stopLineFly();
        let _self = this;
        let positions = pathInfo.map((el, index) => {
            let resultPos = new window.Cesium.Cartesian3.fromDegrees(
                el[0],
                el[1],
                flyHeihgt + el[2]
            );
            return resultPos;
        });
        let allDis = this.getSpaceDistance(positions);
        let start = window.Cesium.JulianDate.fromDate(new Date(2022, 6, 12, 15));
        let totalSeconds = allDis / speed;
        var stop = window.Cesium.JulianDate.addSeconds(
            start,
            totalSeconds,
            new window.Cesium.JulianDate()
        );
        var position = new window.Cesium.SampledPositionProperty();

        this.viewer.clock.startTime = start.clone();
        this.viewer.clock.stopTime = stop.clone();
        this.viewer.clock.currentTime = start.clone();
        this.viewer.clock.clockRange = window.Cesium.ClockRange.LOOP_STOP;
        this.viewer.clock.multiplier = 1;
        let curTime = null;
        positions.forEach((pos, index) => {
            if (index == 0) {
                position.addSample(start, pos);
                curTime = start.clone();
            } 
            // else if(index == positions.length - 1){
            //     this.stopLineFly();
            // } 
            else  {
                let timeStep =
                    (this.getSpaceDistance([pos, positions[index - 1]]) / allDis) *
                    totalSeconds;
                curTime = window.Cesium.JulianDate.addSeconds(
                    curTime,
                    timeStep,
                    new window.Cesium.JulianDate()
                );
                position.addSample(curTime, pos);
                if (index == positions.length - 1) {
                    let timer = setTimeout(() => {
                        this.stopLineFly();
                        clearTimeout(timer)
                    }, totalSeconds * 1000)
                }
            }
        });
        this.lastFlyTime = curTime;
        this.flyEntity = this.viewer.entities.add({
            position: position,
            orientation: new window.Cesium.VelocityOrientationProperty(position),
            model: {
                uri: 'data/fly/wurenji1c.glb',
                minimumPixelSize: 100,
                maximumScale: 200,
                runAnimations: true,
            },
            path: {
                leadTime: 0,
                trailTime: 30,
                resolution: 1,
                material: new window.Cesium.PolylineGlowMaterialProperty({
                    glowPower: 0.1,
                    color: window.Cesium.Color.YELLOW
                }),
                width: 10
            },
        });

        this.flyEntity_yezi = this.viewer.entities.add({
            position: position,
            orientation: new window.Cesium.VelocityOrientationProperty(position),
            model: {
                uri: 'data/fly/wurenji_fengshan.glb',
                minimumPixelSize: 100,
                maximumScale: 200,
                runAnimations: true,
            },
        });
        window.kjycObj = this;
        this.viewer.clock.onTick.addEventListener(this.trackModel);
        this.viewer.clock.shouldAnimate = true;
    }
    // 获取模型位置并追踪
    trackModel() {
        if (window.unlocked) return;
        let _self = window.kjycObj;
        _self.isRoaming = true;
        // 获取当前无人机的实时位置
        let center = _self.flyEntity.position.getValue(
            _self.viewer.clock.currentTime
        );
        if (!center || !center.x) {
            _self.stopLineFly();
            return;
        }
        let orientation = _self.flyEntity.orientation.getValue(
            _self.viewer.clock.currentTime
        );
        let transform = window.Cesium.Transforms.eastNorthUpToFixedFrame(center);
        transform = window.Cesium.Matrix4.fromRotationTranslation(
            window.Cesium.Matrix3.fromQuaternion(orientation),
            center
        );
        _self.viewer.camera.lookAtTransform(
            transform,
            new window.Cesium.Cartesian3(-90, 0, 60)
        );
        // if (
        //     window.Cesium.JulianDate.compare(
        //         _self.viewer.clock.currentTime,
        //         _self.lastFlyTime
        //     ).toFixed(2) >= 0
        // ) {
        //     _self.stopLineFly();
        // }
    }
    // 停止绘制路线漫游
    stopLineFly() {
        this.isRoaming = false;
        this.viewer.clock.shouldAnimate = false;
        this.viewer.camera.lookAtTransform(window.Cesium.Matrix4.IDENTITY); //取消视角锁定
        if (this.flyEntity) {
            this.viewer.entities.remove(this.flyEntity);
            this.flyEntity = null;
            this.viewer.entities.remove(this.flyEntity_yezi);
            this.flyEntity_yezi = null;
        }
        this.viewer.clock.onTick.removeEventListener(this.trackModel);
    }
    // 计算笛卡尔坐标数组空间距离
    getSpaceDistance(positions) {
        var distance = 0;
        for (var i = 0; i < positions.length - 1; i++) {
            var point1cartographic = window.Cesium.Cartographic.fromCartesian(
                positions[i]
            );
            var point2cartographic = window.Cesium.Cartographic.fromCartesian(
                positions[i + 1]
            );
            /**根据经纬度计算出距离**/
            var geodesic = new window.Cesium.EllipsoidGeodesic();
            geodesic.setEndPoints(point1cartographic, point2cartographic);
            var s = geodesic.surfaceDistance;
            //返回两点之间的距离
            s = Math.sqrt(
                Math.pow(s, 2) +
                Math.pow(point2cartographic.height - point1cartographic.height, 2)
            );
            distance = distance + s;
        }
        return distance.toFixed(2);
    }

    clear() {
        this.stopLineFly();
    }

    destroy() {
        this.clear();
    }

}